package com.stream_01;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.lang.Character.isDigit;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;

public class MyTest {

    public static void main(String[] args) {
        List<String> list_1 = Stream.of("aa","ccs","afew").collect(toList());
        boolean b_1 = assertEquals(Arrays.asList("aa", "ccs", "afew"), list_1);
        System.out.println("b_1 = " + b_1);

        List<String> list_2 = Stream.of("aa","ccs","afew")
                .map(string -> string.toUpperCase()).collect(toList());
        boolean b_2 = assertEquals(asList("AA", "CCS", "AFEW"), list_2);
        System.out.println("b_2 = " + b_2);

        List<String> list_3 = Stream.of("1aa","1ccs","afew1")
                .filter(value -> isDigit(value.charAt(0))).collect(toList());
        boolean b_3 = assertEquals(asList("1ccs"), list_3);
        System.out.println("b_3 = " + b_3);

        List<Integer> list_4 = Stream.of(asList(1,2),asList(3,4))
                .flatMap(numbers -> numbers.stream()).collect(toList());
        boolean b_4 = asListToString(asList(1, 2, 3, 4), list_4);
        // boolean b_4 = asListToStream(asList(1, 3, 2, 4), list_4);
        // boolean b_4 = asListToStream_2(asList(1, 3, 2, 4), list_4);
        System.out.println("b_4 = " + b_4);
    }

    private static boolean asListToStream_2(List<Integer> asList, List<Integer> list) {
        List<String> strings = new ArrayList<>(asList.size());
        List<String> strings1 = new ArrayList<>(list.size());
        for (Integer integer : asList) {
            strings.add(String.valueOf(integer));
        }
        for (Integer integer : list) {
            strings1.add(String.valueOf(integer));
        }
        System.out.println(strings.stream().sorted().collect(Collectors.joining()));        // 1234
        return strings.stream().sorted().collect(Collectors.joining())
                .equals(strings1.stream().sorted().collect(Collectors.joining()));
    }

    private static boolean asListToStream(List<Integer> asList, List<Integer> list) {
        String asStr = asList.stream().sorted().collect(toList()).toString();
        String str = list.stream().sorted().collect(toList()).toString();
        System.out.println(str);        // [1, 2, 3, 4]
        return asStr.equals(str);
    }

    /**
     * 使用list转成字符串进行比较是否相等
     * 仅能判断内容，不能判断顺序
     * @param asList
     * @param list
     * @return
     */
    private static boolean asListToString(List<Integer> asList, List<Integer> list) {
        asList.sort(Comparator.comparing(Integer::intValue));
        list.sort(Comparator.comparing(Integer::intValue));
        System.out.println(asList.toString());      // [1, 2, 3, 4]
        return asList.toString().equals(list.toString());
    }

    private static boolean assertEquals(List<String> asList, List<String> list) {
        System.out.println(asList.stream().sorted().collect(Collectors.joining()));     // xxx...
        return asList.stream().sorted().collect(Collectors.joining())
                .equals(list.stream().sorted().collect(Collectors.joining()));
    }
}
